<template>
  <div class="edit_container">
    <van-nav-bar
      title="标题"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />

    <!-- 用户头像 -->
    <div class="avatar">
      <van-image
        round
        width="120"
        height="120"
        fit="cover"
        :src="userInfo.photo"
      />
      <!-- 上传头像 -->
      <van-uploader :after-read="afterRead" />
    </div>

    <van-cell-group>
      <van-cell
        title="名称"
        size="large"
        is-link
        :value="userInfo.name"
        @click="editName"
      />
      <van-cell
        title="性别"
        size="large"
        is-link
        :value="userInfo.gender ? '女' : '男'"
        @click="showPopup = true"
      />
      <van-cell
        title="生日"
        size="large"
        is-link
        :value="userInfo.birthday"
        @click="showBirthday = true"
      />
    </van-cell-group>
    <!-- 编辑名称弹窗 -->
    <van-dialog
      v-model="showDialogName"
      title="编辑昵称"
      show-cancel-button
      @confirm="updateName"
    >
      <van-field v-model="newName" placeholder="请输入名称" ref="name" />
    </van-dialog>

    <!-- 编辑性别弹窗 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="编辑性别"
        left-text="返回"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-cell title="男" is-link @click="updateGender(0)" />
      <van-cell title="女" is-link @click="updateGender(1)" />
    </van-popup>

    <!-- 编辑性别弹窗 -->
    <van-popup v-model="showBirthday" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showBirthday = true"
        @confirm="updateBirthday"
      />
    </van-popup>

    <!-- 裁切图片弹出框 -->
    <div class="cropper_box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120px"
        autoCropHeight="120px"
      ></vueCropper>
      <van-button type="primary" @click="confirmCropper">确认</van-button>
      <van-button type="info" @click="showCropper = false">取消</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { editUserInfo, editAvatar } from "./user.js";
import moment from "moment";

// 导入裁切工具包
import { VueCropper } from "vue-cropper";

export default {
  name: "edit",
  data() {
    return {
      showDialogName: false, //控制编辑姓名弹窗
      newName: "",
      showPopup: false, //控制编辑性别弹窗
      showBirthday: false,
      minDate: new Date(1990, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      img: "", //要裁切的图片
      showCropper: false
    };
  },
  components: {
    VueCropper
  },
  methods: {
    editName() {
      // 弹出窗口
      this.showDialogName = true;
      // 输入框显示当前用户的名字
      this.newName = this.userInfo.name;
      // 等dom更新完才会执行
      this.$nextTick(() => {
        // 输入框指定聚焦
        this.$refs.name.focus();
      });
    },
    // 编辑姓名
    async updateName() {
      let res = await editUserInfo({ name: this.newName });
      console.log(res);
      this.$toast.success("修改成功");
      this.$store.commit("updateUserInfo", {
        ...this.userInfo,
        name: this.newName
      });
    },
    // 编辑性别
    async updateGender(gender) {
      let res = await editUserInfo({ gender });
      this.$store.commit("updateUserInfo", {
        ...this.userInfo,
        gender: gender
      });
      this.showPopup = false;
    },
    // 编辑生日
    async updateBirthday(value) {
      let birthday = moment(value).format("YYYY-MM-DD");
      let res = await editUserInfo({ birthday });
      this.$toast.success("修改成功！");
      this.$store.commit("updateUserInfo", {
        ...this.userInfo,
        birthday: res.data.data.birthday
      });
      this.showBirthday = false;
    },
    afterRead(file) {
      this.img = file.content;
      this.showCropper = true;
    },
    confirmCropper() {
      // 获取截图的blob数据
      this.$refs.cropper.getCropBlob(data => {
        // do something
        // 建立一个空的formData数据
        let fd = new FormData();
        // 将图片的数据添加到formData对象里面
        fd.append("photo", data);
        editAvatar(fd).then(res => {
          // console.log(res);
          this.$toast.success("修改成功");
          this.$store.commit("updateUserInfo", {
            ...this.userInfo,
            photo: res.data.data.photo
          });
        });
        this.showCropper = false;
      });
    }
  },

  computed: {
    ...mapState(["userInfo"])
  }
};
</script>

<style lang="less">
.edit_container {
  .avatar {
    text-align: center;
    padding: 30px 0;
    .van-uploader {
      width: 120px;
      height: 120px;
      position: absolute;
      left: 50%;
      opacity: 0;
      transform: translateX(-50%);
      .van-uploader__wrapper {
        width: 120px;
        height: 120px;
        .van-uploader__upload {
          width: 120px;
          height: 120px;
        }
      }
    }
  }
}
.cropper_box {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  .van-button--primary {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .van-button--info {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>
